# -*- coding: UTF-8 -*-
#安装MYSQL DB for python
import MySQLdb as mdb
import redis
con = None
class PyRedis:
    def __init__(self,host):
        self.host = host
        self.port = 6379
        self.r = redis.StrictRedis(host = self.host, port = self.port)
def yanzhengma(token,type):
    r =PyRedis('10.13.16.202')
    test = r.r.keys("*%s*"%token)
    if len(test) <= 1:
        return 0
    else:
        for data in test:
            split_data = data.split(":")[1]
            if type == 'captcha':
                if split_data == 'captcha':
                    test2 = r.r.hgetall(data)
                    return test2
            elif type == 'identity':
                if split_data == 'identity':
                    identy = r.r.hgetall(data)
                    return identy
        # test1 = test[i]
        # test2 = r.r.hgetall(test1)
def chaxu_uid(val1,val2):
    try:
        con = mdb.connect('10.13.27.242', 'blued', 'ELS5Pt4Hdx3SfGRU', 'blued');
        with con:
            cur = con.cursor()
            #执行一个查询
            state = cur.execute("SELECT uid from users where %s = '%s'"%(val1,val2))
            #取得上个查询的结果，是单个结果
            result = cur.fetchone()
            return result,state
    finally:
        con.close()
def change_wandou(parameter1,parameter2,parameter3):
    try:
        con = mdb.connect('10.13.27.242', 'blued', 'ELS5Pt4Hdx3SfGRU', 'blued');
        with con:
            cur = con.cursor()
            #执行修改语句
            rel = cur.execute("select uid from users_ios_sandbox where uid = '%s'"%parameter2)
            if rel == 1 and parameter3=='ios':
                parameter3 = 'iossandbox'
            state = cur.execute("update users_sums set beans='%s' where uid='%s' and platform = '%s'"%(parameter1,parameter2,parameter3))
            print state
            try:
                if state == 0:
                    re = cur.execute("select beans from users_sums where (uid ='%s' and platform = '%s')"%(parameter2,parameter3))
                    re = str(re)
                    if re == '0':
                        state = cur.execute("insert INTO users_sums (beans, uid, platform) VALUES ('%s', '%s', '%s')"%(parameter1,parameter2,parameter3))
                    else:
                        bean = str(re[1])[:-2]
                        if re[0]==parameter3 and bean==parameter1:
                            state ==0
            except:
                state == 0
            return state
    finally:
        con.close()
def i_change_wandou(parameter1,parameter2,parameter3):
    try:
        con = mdb.connect('10.13.27.242', 'blued', 'ELS5Pt4Hdx3SfGRU', 'blued_oversea');
        with con:
            cur = con.cursor()
            #执行修改语句
            rel = cur.execute("select uid from users_ios_sandbox where uid = '%s'"%parameter2)
            if rel == 1 and parameter3=='ios':
                parameter3 = 'iossandbox'
            state = cur.execute("update users_sums set beans='%s' where uid='%s' and platform = '%s'"%(parameter1,parameter2,parameter3))
            try:
                if state == 0:
                    re = cur.execute("select beans from users_sums where (uid ='%s' and platform = '%s')"%(parameter2,parameter3))
                    re = str(re)
                    if re == '0':
                        state = cur.execute("insert INTO users_sums (beans, uid, platform) VALUES ('%s', '%s', '%s')"%(parameter1,parameter2,parameter3))
                    else:
                        bean = str(re[1])[:-2]
                        if re[0]==parameter3 and bean==parameter1:
                            state ==0
            except:
                state == 0
            return state
    finally:
        con.close()
def chaxun_yanzhengma():
    try:
        con = mdb.connect('10.13.5.157', 'root', 'qatest', 'qa');
        with con:
            cur = con.cursor()
            cur.execute("truncate table qa_identity");
            r =PyRedis('10.13.16.202')
            test = r.r.keys("*psp:identity*")
            for i in range(len(test)):
                test1 = test[i]
                test2 = r.r.hgetall(test1)
                pre_auth = ''
                pre_type = ''
                pre_iden = ''
                for (d,x) in test2.items():
                    if d == 'authcode':
                        auth = x;
                    elif d == 'type':
                        type = x;
                    elif d == 'identity':
                        iden = x;
                    elif d == 'pre_authcode' :
                        pre_auth = str(x);
                    elif d == 'pre_type':
                        pre_type = str(x);
                    elif d == 'pre_identity':
                        pre_iden = str(x);
                #执行一个查询
                if pre_auth != '' and pre_type != '' and pre_iden != '':
                    cur.execute("insert INTO qa_identity (authc, acount, ident) VALUES ('%s', '%s', '%s')"%(pre_auth,pre_type,pre_iden))
                try:
                    cur.execute("insert INTO qa_identity (authc, acount, ident) VALUES ('%s', '%s', '%s')"%(auth,type,iden))
                except:
                    pass
    finally:
        con.close()
def change_star(star_uid,value):
    r =PyRedis('10.13.16.202')
    star_uid = str(star_uid)
    num = str(value)
    star_result = r.r.hset('u:%s'%star_uid,'vbadge','%s'%num)
    return star_result
def change_payment(uid,ver):
    r =PyRedis('10.13.16.202')
    uid = str(uid)
    code = '4077bca072b7332f8f68a216441e403d111036e6'
    salt = '88888888'
    if ver == 0:  #0为国内版
        paycode = 'payment_code'
        paysalt = 'payment_salt'
        db = "blued"
    else:
        db = "blued_oversea"
        paycode = 'payment_code_oversea'
        paysalt = 'payment_salt_oversea'
    try:
        con = mdb.connect('10.13.27.242', 'blued', 'ELS5Pt4Hdx3SfGRU', db)
        with con:
            cur = con.cursor()
            #执行一个查询
            state = cur.execute("update users_payment_code SET code = '%s',salt = '%s' where uid = '%s'"%(code,salt,uid))
            result1 = r.r.hset('u:%s'%uid,paycode,code)
            result2 = r.r.hset('u:%s'%uid,paysalt,salt)
            #取得上个查询的结果，是单个结果
            result = cur.fetchone()
            return state,result1,result2
    finally:
        con.close()
   # return star_result